package com.ykdl.member.kid.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ykdl.member.kid.beans.DoctorTalkChildListBean;

import net.wxxr.http.interfaces.IJsonParser;

public class HomeBean implements Serializable, IJsonParser {
	private static final long serialVersionUID = -254075575073094016L;

	// {
	// +doctor_talks: [ … ]
	// +group_catalogs: [ … ]
	// +top_ads: [ … ]
	// +topics: [ … ]
	// }
	//
	/** 资讯分类(AdItemBean)集合, 用户显示首页顶部图片轮播 **/
	private List<AdItemBean> top_ads = null;

	/** 医生说育儿 (包含文章和诊所精选) **/
	/** doctor_talks **/
	private List<ArticleBean> articles = null; // 文章
	private List<CatalogBean> catalogs = null; // 诊所精选

	/** 医生说育儿 **/
	private List<Object> objs = null;

	/** 热图榜 **/
	private List<TopicBean> topics = null;

	/** 辣妈聊育儿 话题 **/
	private List<CatalogBean> group_catalogs = null;
	
	private boolean isExpired = false;
	

	public boolean isExpired() {
		return isExpired;
	}

	public void setExpired(boolean isExpired) {
		this.isExpired = isExpired;
	}

	public List<AdItemBean> getTop_ads() {
		if (top_ads == null) {
			top_ads = new ArrayList<AdItemBean>();
		}

		return top_ads;
	}

	public void setTop_ads(List<AdItemBean> top_ads) {
		this.top_ads = top_ads;
	}

	public List<ArticleBean> getArticles() {
		if (articles == null) {
			articles = new ArrayList<ArticleBean>();
		}
		return articles;
	}

	public void setArticles(List<ArticleBean> articles) {
		this.articles = articles;
	}

	public List<CatalogBean> getCatalogs() {
		if (catalogs == null) {
			catalogs = new ArrayList<CatalogBean>();
		}
		return catalogs;
	}

	public void setCatalogs(List<CatalogBean> catalogs) {
		this.catalogs = catalogs;
	}

	public List<TopicBean> getTopics() {
		if (topics == null) {
			topics = new ArrayList<TopicBean>();
		}
		return topics;
	}

	public void setTopics(List<TopicBean> topics) {
		this.topics = topics;
	}

	public List<CatalogBean> getGroup_catalogs() {
		if (group_catalogs == null) {
			group_catalogs = new ArrayList<CatalogBean>();
		}
		return group_catalogs;
	}

	public void setGroup_catalogs(List<CatalogBean> group_catalogs) {
		this.group_catalogs = group_catalogs;
	}

	public List<Object> getObjs() {
		if (objs == null) {
			objs = new ArrayList<Object>();
		}
		return objs;
	}

	public void setObjs(List<Object> objs) {
		this.objs = objs;
	}

	@Override
	public IJsonParser parse(String json) throws Exception {
		Gson gson = null;
		if (null != json) {
			GsonBuilder b = new GsonBuilder();
			gson = b.create();
			final HomeBean bean = new HomeBean();
			if("null".equals(json))return bean;
			if("{}".equals(json) || "[]".equals(json))return bean;
			
			if(json.startsWith("{") && json.endsWith("}")
					&& new JSONObject(json).has("error")
					&& new JSONObject(json).getInt("error") == 20007){
				bean.setExpired(true);
				
				return bean;
			}
				
			JSONObject object = new JSONObject(json);
			JSONArray top_ads_json_array =null;
			JSONArray doctor_talks_json_array = null;
			JSONArray group_catalogs_json_array = null;
			JSONArray topics_json_array = null;
			// 图片轮播
			if (!isJsonTagNull(object, "top_ads"))
					top_ads_json_array = object.getJSONArray("top_ads");
			// 医生说育儿
			if (!isJsonTagNull(object, "doctor_talks"))
					doctor_talks_json_array = object.getJSONArray("doctor_talks");
			// 辣妈聊育儿
			if (!isJsonTagNull(object, "group_catalogs"))
			 group_catalogs_json_array = object
					.getJSONArray("group_catalogs");
			// 热图榜
			if (!isJsonTagNull(object, "topics"))
			 topics_json_array = object.getJSONArray("topics");

			// 组建图片轮播集合
			
			if (top_ads_json_array != null && top_ads_json_array.length() > 0) {
				for (int i = 0; i < top_ads_json_array.length(); i++) {
					String top_ad_json = top_ads_json_array.getString(i);
					if (top_ad_json == null || top_ad_json.equals("{}"))
						continue;
					AdItemBean aditem = gson.fromJson(top_ad_json,
							AdItemBean.class);
					bean.getTop_ads().add(aditem);
				}
			}

			// 组建医生说育儿数据
			if (doctor_talks_json_array != null
					&& doctor_talks_json_array.length() > 0) {
				setDoctorTalkChildData(gson, bean, doctor_talks_json_array);
			}

			// 组建辣妈聊育儿
			if (group_catalogs_json_array != null
					&& group_catalogs_json_array.length() > 0) {
				setFreakyChatChildData(gson, bean, group_catalogs_json_array);
			}

			// 组建热图榜
			if (topics_json_array != null && topics_json_array.length() > 0) {
				for (int i = 0; i < topics_json_array.length(); i++) {
					if (null == topics_json_array.getString(i)
							|| topics_json_array.getString(i).equals("{}"))
						continue;
					bean.getTopics().add(
							gson.fromJson(topics_json_array.getString(i),
									TopicBean.class));
				}
			}

			return bean;
		}
		return null;

	}

	/**
	 * 辣妈聊育儿
	 * 
	 * @param gson
	 * @param bean
	 * @param group_catalogs_json_array
	 * @throws JSONException
	 */
	private void setFreakyChatChildData(Gson gson, HomeBean bean,
			JSONArray group_catalogs_json_array) throws JSONException {
		for (int i = 0; i < group_catalogs_json_array.length(); i++) {
			final String catalog_item = group_catalogs_json_array.getString(i);
			if (catalog_item == null || catalog_item.equals("{}"))
				continue;
			JSONObject jo = new JSONObject(catalog_item);
			if (jo.has("catalog_id") && !jo.isNull("catalog_id")) {
				int catalog_type = jo.getInt("catalog_type");
				final String catalog_items = jo.getString("items");
				//if (catalog_items == null || catalog_items.equals("[]"))
				//	continue;
				List<CatalogItemBean> item = null;
				if (catalog_type == 1) { // 话题(catalog_type == 1) 或
											// 问题(catalog_type == 3)
					item = gson.fromJson(catalog_items,
							new TypeToken<List<CatalogTopicItem>>() {
							}.getType());
					//if (null == ((CatalogTopicItem) item.get(0)).getItem())
					//	continue;
				}
				// else if(jo.getInt("catalog_type") == 2){ //文章
				// item = gson.fromJson(catalog_items, new
				// TypeToken<List<CatalogArticleItem>>(){}.getType());
				// if(null == ((CatalogArticleItem)item.get(0)).getItem())
				// continue;
				// }
				final CatalogBean catalog = gson.fromJson(catalog_item,
						CatalogBean.class);

				if (item != null) {
					catalog.setCatalogItems(item);
				}

				bean.getCatalogs().add(catalog);
			}
		}
	}

	/**
	 * 医生说育儿
	 * 
	 * @param gson
	 * @param bean
	 * @param doctor_talks_json_array
	 * @throws JSONException
	 */
	private void setDoctorTalkChildData(Gson gson, final HomeBean bean,
			JSONArray doctor_talks_json_array) throws JSONException {
		for (int i = 0; i < doctor_talks_json_array.length(); i++) {
			final String doctor_talk_item = doctor_talks_json_array
					.getString(i);
			if (doctor_talk_item == null || doctor_talk_item.equals("{}"))
				continue;
			JSONObject jo = new JSONObject(doctor_talk_item);
			if (jo.has("article_id") && !jo.isNull("article_id")) {
				bean.getObjs().add(gson.fromJson(doctor_talk_item, ArticleBean.class));
			} else if (jo.has("catalog_id") && !jo.isNull("catalog_id")) {
				//int catalog_type = jo.getInt("catalog_type");
				final String catalog_items = jo.getString("items");
				//if (catalog_items == null || catalog_items.equals("[]"))
				//	continue;
				List<CatalogItemBean> item = null;
				//if (catalog_type == 2) { //问医生
					item = gson.fromJson(catalog_items,
							new TypeToken<List<CatalogTopicItem>>() {
							}.getType());
					//if (null == ((CatalogTopicItem) item.get(0)).getItem())
					//	continue;
				//} else if (jo.getInt("catalog_type") == 200) { // 预留数据，暂不处理
				//	item = gson.fromJson(catalog_items,
				//			new TypeToken<List<CatalogArticleItem>>() {
				//			}.getType());
					//if (null == ((CatalogArticleItem) item.get(0)).getItem())
					//	continue;
			//	}

				final CatalogBean catalog = gson.fromJson(doctor_talk_item,
						CatalogBean.class);

				if (item != null) {
					catalog.setCatalogItems(item);
				}
				bean.getObjs().add(catalog);
			}
		}
	}
	
	
	private boolean isJsonTagNull(JSONObject object, final String tag)throws JSONException{
		
		if (null != object.getString(tag)
				&& !"".equals(object.getString(tag))
				&& !"null".equals(object.getString(tag))){
			return false;
		}else{
			return true;
		}
	}

}
